package com.tliaswebmanagement.service.impl;

import com.tliaswebmanagement.aop.Log;
import com.tliaswebmanagement.mapper.DeptMapper;
import com.tliaswebmanagement.mapper.EmpMapper;
import com.tliaswebmanagement.pojo.Dept;
import com.tliaswebmanagement.pojo.DeptLog;
import com.tliaswebmanagement.service.DeptLogService;
import com.tliaswebmanagement.service.DeptService;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;

    /**
     * 查询部门
     *
     * @return
     */
    @Log
    @Override
    public List<Dept> list() {
        List<Dept> list = deptMapper.list();
        return list;
    }

    /**
     * 添加部门
     *
     * @param dept
     */
    @Override
    public void add(Dept dept) {
        deptMapper.add(dept);
    }

    /**
     * 查询id
     *
     * @param id
     * @return
     */
    @Override
    public Dept getById(Integer id) {
        Dept dept = deptMapper.getById(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        deptMapper.update(dept);
    }

    /**
     * 根据id删除部门
     *
     * @param id
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) throws FileNotFoundException {
        try {
            deptMapper.detele(id);
            new FileOutputStream("");
            empMapper.deleteDeptId(id);
        } finally {
            DeptLog deptLog = new DeptLog();
            deptLog.setCreateTime(LocalDateTime.now());
            deptLog.setDescription("删除了" + id + "部门");
            deptLogService.insert(deptLog);
        }
    }
}
